package com.wish.g04.base.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.dto.JrFactoringDTO;
import com.wish.g04.base.entity.CwReceiptPlan;
import com.wish.g04.base.entity.JrProduct;
import com.wish.g04.base.mapper.CwReceiptPlanMapper;
import com.wish.g04.base.service.ICwReceiptPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.g04.base.service.IJrProductService;
import com.wish.g04.base.vo.FactoringDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收款计划表 服务实现类
 * </p>
 *
 * @author 薛凯
 * @since 2022-05-23
 */
@Service
public class CwReceiptPlanServiceImpl extends ServiceImpl<CwReceiptPlanMapper, CwReceiptPlan> implements ICwReceiptPlanService {

    @Autowired
    private IJrProductService iJrProductService;

    /**
     * 根据合同id查询收款计划
     *
     * @return
     */
    @Override
    public List<FactoringDetailVO> selectByContractId(JrFactoringDTO jrFactoringDTO) {
        QueryWrapper<CwReceiptPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flg",0)
                //收款状态：未收款的
                .eq("sk_status",1)
                // 账单金融属性：普通账单(正常房租租务计划)
                .eq("bill_type",4)
                // 款项：租金
                .eq("item_type",3)
                // 收款类型：房租收款
                .eq("receipt_type",1)
                // 未逾期的：应付日期大于等于当前日期，预定收款日期大于当期日期，也就是说正常未断供的
                .gt("collection_date", LocalDateTime.now())
                // 合同Id
                .eq("contract_id",jrFactoringDTO.getContractId())
                // 合同类型为出房合同
                .eq("contract_type",2)
                .orderByDesc("receipt_number");
        // 查询金融产品
        JrProduct product = iJrProductService.getById(jrFactoringDTO.getJrProductId());
        // 查询详情
        List<CwReceiptPlan> list = list(wrapper);
        List<FactoringDetailVO> factoringDetailVOS = new ArrayList<>();
        for (CwReceiptPlan one : list) {
            FactoringDetailVO factoringDetailVO = new FactoringDetailVO();
            BeanUtils.copyProperties(one,factoringDetailVO);
            // 应收租约金额
            factoringDetailVO.setShouldMonthRent(one.getPaymentAmount());
            // 授信金额 = 应收租约金额*(授信额度/100) (备注：四舍五入)
            factoringDetailVO.setFactoringCreditMoney(one.getPaymentAmount().multiply(product.getCreditLine()));
            // 账期开始
            factoringDetailVO.setBorrowedStart(one.getPaymentDaysBegin());
            // 账期结束
            factoringDetailVO.setBorrowedEnd(one.getPaymentDaysEnd());
            // 周期
            DateTime start = DateUtil.parse(DateUtil.format(factoringDetailVO.getBorrowedStart(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            DateTime end = DateUtil.parse(DateUtil.format(factoringDetailVO.getBorrowedEnd(), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            long period = DateUtil.betweenMonth(start, end, false);
            factoringDetailVO.setFactoringPeriod((int)period);
            // 服务费 = 保理授信金额*服务费率/12*月数(备注：四舍五入)
            factoringDetailVO.setServiceMoney(factoringDetailVO.getFactoringCreditMoney()
                    .multiply(new BigDecimal(1/(100 * 12)))
                    .multiply(new BigDecimal(factoringDetailVO.getFactoringPeriod()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP));
            // 利息 = 保理授信总金额 *月利率 *月数  = 保理授信总金额 * 年利率/12 *月数 (备注：四舍五入)
            factoringDetailVO.setFactoringInterest(factoringDetailVO.getShouldMonthRent()
                    .multiply(new BigDecimal(jrFactoringDTO.getRate() / (100 * 12)))
                    .multiply(new BigDecimal(factoringDetailVO.getFactoringPeriod()))
                    .setScale(1,BigDecimal.ROUND_HALF_UP));
            factoringDetailVOS.add(factoringDetailVO);
        }

        return factoringDetailVOS;
    }
}
